package com.darrenchan.leetcode;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-11-24 21:00
 */
public class Q38 {
    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        String pre = "1";
        for (int i = 2; i <= n; i++) {
            pre = getNext(pre);
        }
        return pre;
    }

    private String getNext(String str) {
        str += "0";//只是为了方便后面的判断
        StringBuilder result = new StringBuilder();
        int count = 1;
        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) == str.charAt(i - 1)) {
                count++;
            } else {
                result.append(count);
                result.append(str.charAt(i - 1));
                count = 1;
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Q38().getNext("1211"));
        System.out.println(new Q38().countAndSay(8));
    }
}
